<?php
namespace Home\Controller;
header("content-type:text/html;charset=utf-8");
use Think\Controller;
/*管理员登录访问权限*/
class Admin1Controller extends controller{
    /*管理员身份确认*/
    /*与指定的管理员账号密码验证*/
    function test(){
        $db=M('user_reg');
        $userreg['username']=I('post.username');
        $arr=$db->where($userreg)->find();
        $rand=$arr['rand'];
        $userreg['password']=MD5(I('post.password').$rand);
        $arr1=$db->where($userreg)->find();
        $re=count($arr1);
        if($re==0){
            echo 0;/*登录失败*/
        }elseif($arr1['is_admin']==1){
            $token=MD5(rand(1000,9999));
            cookie('tk',$token,600);
            session($token,$arr1['username']);
            echo 1;/*登录成功*/
        }
    }

    /*审核处理*/
    function aprocess(){
        if(isset($_GET['act'])){
            $act=$_GET['act'];/*获取操作类型*/
            $id=$_GET['id'];
            if($act==1){
                $uart=M('user_addarticle');
                $ids['id']=$id;
                $pass['is_pass']=1;
                $pass['state']="已通过";
                $uart->where($ids)->save($pass);
                $this->success('您同意了该用户的发布请求，页面跳转中',U('Admin/myuser'),2);
            }elseif($act==0){
                $uart=M('user_addarticle');
                $ids['id']=$id;
                $uart->where($ids)->delete();
                $this->success('您拒绝并删除了该用户的发布请求，页面跳转中',U('Admin/myuser'),2);
            }
        }
    }

    /*用户添加文章提交后台*/
    function art_apply()
    {
        if (isset($_POST['submit'])) {
            $uid=$_GET['uid'];/*新表中的uid*/
            $db_art=M('user_addarticle');
            if (isset($_FILES)) {
                $fname = $_FILES["file"]["name"];//上传文件名
                $fname = iconv('utf-8', 'gb2312', $fname);
                $ftype = $_FILES["file"]["type"];
                $lname = substr($fname, strpos($fname, "."));//获取文件后缀名
                /*设置上传文件类型*/
                $flag = 0;
                switch ($lname) {
                    case ".jpg" :
                        $flag = 1;
                        break;
                    case ".gif":
                        $flag = 1;
                        break;
                    case ".jpeg":
                        $flag = 1;
                        break;
                    case ".png":
                        $flag = 1;
                        break;
                };
                switch ($ftype) {
                    case "image/jpg":
                        $flag++;
                        break;
                    case "image/gif":
                        $flag++;
                        break;
                    case "image/jpeg":
                        $flag++;
                        break;
                    case "image/png":
                        $flag++;
                        break;
                };
                if($flag==2) {
                    $fload = move_uploaded_file($_FILES["file"]["tmp_name"], "Public/images/".$fname);
                    if ($fload) {
                        $fileurl="images/".$fname;
                    } else {
                        echo "错误代码:" . $_FILES["file"]["error"];
                    }
                }else{
                    echo "操作失败，请重试！";
                }
            }
            $art['headline']=I('post.headline');/*文章标题*/
            $art['headurl']=$fileurl;/*文章图片路径*/
            $art['instruct']=I('post.instruct');/*文章介绍*/
            $art['content']=I('post.content');/*文章内容*/
            if($uid=="admin"){
                $u_reg=M('user_reg');
                $username['username']=$uid;
                $re=$u_reg->where($username)->find();
                $art['uid']=$re['id'];
                $art['mail']=$re['mail'];
                $db_art->add($art);
                $this->success('信息发布成功',U('Admin/showarticle'),2);
            }else{
                $art['uid']=$uid;/*用户id*/
                $u_reg=M('user_reg');
                $re=$u_reg->where("id=$uid")->find();
                $art['mail']=$re['mail'];/*用户邮箱*/
                $db_art->add($art);
                $this->success('审核中',U('User/showarticle'),2);
                }

        }
    }
    function art_change()
    {
        if (isset($_POST['submit'])) {
            $aid=$_GET['aid'];
            $db_art=M('user_addarticle');
            if (isset($_FILES)) {
                $fname = $_FILES["file"]["name"];//上传文件
                if(empty($fname)){
                    $art['headline']=I('post.headline');/*文章标题*/
                    $art['instruct']=I('post.instruct');/*文章介绍*/
                    $art['content']=I('post.content');/*文章内容*/
                    $db_art->where("id=$aid")->save($art);
                    $this->success('等待管理员审核',U('User/showarticle'),2);
                }else{
                    $fname = iconv('utf-8', 'gb2312', $fname);
                    $ftype = $_FILES["file"]["type"];
                    $lname = substr($fname, strpos($fname, "."));//获取文件后缀名
                    /*设置上传文件类型*/
                    $flag = 0;
                    switch ($lname) {
                        case ".jpg" :
                            $flag = 1;
                            break;
                        case ".gif":
                            $flag = 1;
                            break;
                        case ".jpeg":
                            $flag = 1;
                            break;
                        case ".png":
                            $flag = 1;
                            break;
                    };
                    switch ($ftype) {
                        case "image/jpg":
                            $flag++;
                            break;
                        case "image/gif":
                            $flag++;
                            break;
                        case "image/jpeg":
                            $flag++;
                            break;
                        case "image/png":
                            $flag++;
                            break;
                    };
                    if($flag==2) {
                        $fload = move_uploaded_file($_FILES["file"]["tmp_name"], "Public/images/".$fname);
                        if ($fload) {
                            $fileurl="images/".$fname;
                        } else {
                            echo "错误代码:" . $_FILES["file"]["error"];
                        }
                    }else{
                        echo "操作失败，请重试！";
                    }

                    $art['headline']=I('post.headline');/*文章标题*/
                    $art['headurl']=$fileurl;/*文章图片路径*/
                    $art['instruct']=I('post.instruct');/*文章介绍*/
                    $art['content']=I('post.content');/*文章内容*/
                    $db_art->where("id=$aid")->save($art);
                    $this->success('等待管理员审核',U('User/showarticle'),2);
                }
            }
        }
    }
}